//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/12.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        // 判断是否是新版本
//        isNewVersion()
        
        // 获取用户信息数据
        let account = UserAccount.loadAccount()
        print(account)
        
        // 创建自定义window 手动实例化
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        // 设置跟视图控制器
        window?.rootViewController = defalutViewController()
        
        // 方便测试: 直接跳转发微博界面
//        let nav = UINavigationController(rootViewController: PictureSelectViewController())
//        window?.rootViewController = nav
        
        // 设置UINavigationBar和UITabBar的颜色
        setThemeColor()
        
        // 注册通知
        registerNotification()
        
        return true
    }
    
    // MARK: 注册通知
    // object如果不为空则 对应: 通知,   nil对应: 广播
    private func registerNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: WBSwitchRootVCNotification, object: nil)
    }
    
    // 移除通知
    //单例对象 程序一运行 就会建立 Appdelegate单例对象  直到程序销毁的时候 才会调用
    //写与不写 没有本质上的区别  只是一种编码习惯而已
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // 通知监听的方法
    @objc private func switchRootVC(n: NSNotification) {
        print(n)
        // 判断 object 是否为 nil
        if n.object != nil {
            // 如果 object 不为nil 跳转到欢迎界面
            window?.rootViewController = WelcomeViewController()
            return
        }
        // object不为nil  跳转到首页
        window?.rootViewController = MainViewController()
    }
    
    
    // MARK: 根据用户是否登录, 从而显示相应的界面
    private func defalutViewController() -> UIViewController {
        // 判断用户是否登录
        if UserAccountViewModel().userLogin {
            // 如果登录  则判断新旧版本
            return isNewVersion() ? NewFeatureViewController() : WelcomeViewController()
        }
        // 如果没有登录 则返回主机界面,显示访客视图
        return MainViewController()
    }
    
    
    // MARK: 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 获取当前应用版本(新版本)
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)
//        print(currentVersion)
        
        // 获取本地缓存版本(老版本)
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let sandBoxKey = "sandBoxKey"
        let lastVersion = userDefaults.doubleForKey(sandBoxKey)
        // 存入当前版本号
        userDefaults.setDouble(currentVersion!, forKey: sandBoxKey)
        userDefaults.synchronize()
        
        // 比较新老版本号
        
        return currentVersion! > lastVersion
    }

    private func setThemeColor() {
        
        // 设置tabbar颜色
        UITabBar.appearance().tintColor = themeColor
        // 设置导航栏按钮颜色
        UINavigationBar.appearance().tintColor = themeColor
        // 设置导航栏颜色
//        UINavigationBar.appearance().barTintColor = themeColor
        // 设置标题颜色
//        UINavigationBar.appearance().titleTextAttributes = NSDictionary(object: themeColor, forKey: NSForegroundColorAttributeName) as? [String : AnyObject]
        
    }
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

